package DesignPatern.A08_调停者模式.A002_调停者模式解决;

/**
 * @Author: Du
 * @Date: 2021/7/23 9:14
 */
public class Mediator extends AbstractMediator
{
    @Override
    public void execute(String str, Object... objects)
    {
        if (str.equalsIgnoreCase("purchase"))
        {
            this.buyComputer((Integer) objects[0]);
        } else if (str.equalsIgnoreCase("sale"))
        {
            this.sellComputer((Integer) objects[0]);
        } else if (str.equalsIgnoreCase("offsell"))
        {
            this.offSell();
        } else if (str.equalsIgnoreCase("clear"))
        {
            this.clearStock();
        } else if (str.equalsIgnoreCase("query"))
        {
            this.getStockNumber();
        }
    }
    
    public void getStockNumber()
    {
        System.out.println("剩余电脑数量：" + super.stock.getComputerNumber());
    }
    
    public void buyComputer(int number)
    {
        int saleStatus = super.sale.getSaleStatus();
        if (saleStatus > 80)
        {
            System.out.println("售价>80，全额全部电脑" + number + "台");
        } else
        {
            int buyNumber = number / 2;
            System.out.println("售价<80，购买一半电脑" + buyNumber + "台");
        }
    }
    
    public void sellComputer(int number)
    {
        if (super.stock.getComputerNumber() < number)
        {
            System.out.println("库存不足，补仓" + number + "台");
            super.purchase.buyComputer(number);
        }
        System.out.println("卖出电脑" + number + "台");
        super.stock.decrease(number);
    }
    
    public void offSell()
    {
        System.out.println("折价售货:" + super.stock.getComputerNumber());
    }
    
    private void clearStock()
    {
        super.sale.OffSale();
        super.purchase.refuseIBM();
    }
}
